package com.pu.controller.user;

import com.pu.context.BaseContext;
import com.pu.dto.ShoppingCartDTO;
import com.pu.entity.ShoppingCart;
import com.pu.result.Result;
import com.pu.service.ShoppingCartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author Yusc
 * @Date 2023/10/31 23:15
 * @Version
 * @Description C端-购物车
 **/
@RestController
@RequestMapping("/user/shoppingCart")
@Slf4j
@Api(tags = "C端-购物车接口")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * @param shoppingCartDTO 前端接收的数据
     * @auther: Yusc
     * @date 2023/10/31 23:17
     * @Description 添加购物车
     */
    @PostMapping("/add")
    @ApiOperation("添加购物车")
    public Result add(@RequestBody ShoppingCartDTO shoppingCartDTO) {
        log.info("添加购物车，商品的信息为：{}", shoppingCartDTO);

        shoppingCartService.addShoppingCart(shoppingCartDTO);

        return Result.success();
    }

    /**
     * @auther: Yusc
     * @date 2023/11/1 16:15
     * @Description 查看购物车
     */
    @GetMapping("/list")
    @ApiOperation("查看购物车")
    public Result<List<ShoppingCart>> list() {
        log.info("用户查看了购物车!用户id为:{}", BaseContext.getCurrentId());
        return Result.success(shoppingCartService.showShoppingCart());
    }

    /**
     * @auther: Yusc
     * @date 2023/11/1 16:30
     * @Description 删除购物车中一个商品
     */
    @PostMapping("/sub")
    @ApiOperation("删除购物车中一个商品")
    public Result sub(@RequestBody ShoppingCartDTO shoppingCartDTO) {
        log.info("删除购物车中一个商品，商品：{}", shoppingCartDTO);
        shoppingCartService.subShoppingCart(shoppingCartDTO);
        return Result.success();
    }

    /**
    * @auther: Yusc
    * @date 2023/11/1 16:38
    * @Description 清空购物车
    */
    @DeleteMapping("/clean")
    @ApiOperation("清空购物车商品")
    public Result<String> clean(){
        shoppingCartService.cleanShoppingCart();
        return Result.success();
    }
}
